package thread;

import java.util.Scanner;

public class ThreadStop {
	private static boolean run = true;
	public static void main(String[] args) {
		System.out.println(Thread.activeCount());
		test();
		System.out.println(Thread.activeCount());
	}

	private static void test() {
		Thread t1 = new Thread(){
			@Override
			public void run() {
				for(int i=0;run;){
					System.out.println(i++);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		Thread t2 = new Thread(){
			@Override
			public void run() {
				Scanner in = new Scanner(System.in);
				while(run){
					String s=in.nextLine();//阻塞程序的方法
					if(s.equals("stop")){
						run=false;
					}
				}
				
//				System.out.println("byebye");
			}
		};
		Thread t3 = new Thread(){
			@Override
			public void run() {
				while(run){
					System.out.println("stop me!");
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println("thank you!!");
				System.out.println("byebye");
			}
		};
		t1.start();
		t2.start();
		t3.start();
	}
}
